package com.yowasa.xiuxian.engine.message.activity;

import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.MysWareHouse;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.RedisService;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.WareHoustHelper;
import com.yowasa.xiuxian.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;


@CMD(name = "/仓库")
public class WarehouseTestHandler extends CmdMessageHandler {
    public static final Logger logger = LoggerFactory.getLogger(WarehouseTestHandler.class);

    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private WareHoustHelper wareHoustHelper;


    @Override
    public void execute(SendMessage message) {
        String userId = message.getFrom_user_id();

        List<String> params = getAllParam(message);//获取名称
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(params.get(0))) {
            messageHelper.replyMys(message, "可以在后面追加 添加 减少 查看 等二级指令进行后续操作");
            return;
        }
        if ("查看".equals(params.get(0))) {
            String s;
            try {
                s = params.get(1);
            }catch (Exception e){
                s="";
            }
            List<MysWareHouse> allByUserId = wareHoustHelper.getAllByUserId(userId,s);
            String msg = wareHouseDesc(allByUserId);
            messageHelper.replyMys(message,msg);

        }
        if ("添加".equals(params.get(0)) || "减少".equals(params.get(0))) {
            String flag = wareHoustHelper.checkAdmin(userId,1);
            if(flag == null){
                throw new HandlerException("您没有此指令的权限");
            }

            String atUserId = messageHelper.getMysContentAtUserId(message.getContent());
            if (StringUtils.isEmpty(atUserId)) {
                throw new HandlerException("需要at对方才可修改物品数量");
            }

            //添加 物品 数量
            if(params.size() < 3){
                throw new HandlerException("正确指令为 /添加 物品 数量 @成员");
            }
            String itemName = params.get(1);
            Integer num = StrUtil.checkAndConvert2Int(params.get(2), "数量");
            if ("添加".equals(params.get(0))) {
                wareHoustHelper.addItem(flag,atUserId,itemName,num);
            }

            if("减少".equals(params.get(0))) {
                if(!wareHoustHelper.reduceItem(flag,atUserId,itemName,num)){
                    throw new HandlerException("物品数量不足，无法扣减");
                }
            }
            messageHelper.replyMys(message,"操作成功！");
        }

    }

    private String wareHouseDesc(List<MysWareHouse> allByUserId) {
        StringBuilder sb = new StringBuilder();
        sb.append("格式西风：  [仓库名] 物品名 * 数量\n");
        allByUserId.forEach(e -> {
            sb.append("[").append(e.getFlag()).append("]  ").append(e.getItemName()).append(" * ").append(e.getItemNum()).append("\n");
        });
        return sb.toString();
    }


    public String desc() {
        return "本指令与游戏无关，供有需求的别野管理使用，可创建自己别野的专属仓库，自定义物品名称，存储用户与物品关系数据，如需使用请联系开发者\n具体使用方式为 @机器人 /仓库 添加或减少 物品名 数量 @成员\n成员可以使用 /仓库 查看 来展示自己当前所有物品";
    }


}
